package com.zwps.biz.domain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwps.biz.api.model.vo.structure.*;
import com.zwps.biz.dal.db.dao.TDepartmentDAO;
import com.zwps.biz.dal.db.dao.TDepartmentWorkerDAO;
import com.zwps.biz.dal.db.object.TDepartmentDO;
import com.zwps.biz.dal.db.object.TDepartmentWorkerDO;
import com.zwps.biz.dal.db.object.ZDeputyActivityRecordDO;
import com.zwps.biz.domain.service.TDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 *
 */
@DS("one")
@Service
public class TDepartmentServiceImpl extends ServiceImpl<com.zwps.biz.dal.db.dao.TDepartmentDAO, TDepartmentDO>
        implements TDepartmentService {
    @Autowired
    private TDepartmentDAO departmentDAO;
    @Autowired
    private TDepartmentWorkerDAO workerDAO;

    @Override
    public TDepartmentStruVO getDate() {

        TDepartmentStruVO departmentStruVo = new TDepartmentStruVO();

        Optional<TDepartmentDO> departmentOptional = Optional.ofNullable(
                this.getOne(new QueryWrapper<TDepartmentDO>().lambda().eq(TDepartmentDO::getDepartmentId, 0).eq(TDepartmentDO::getIsDelete, 0))
        );
        departmentStruVo.setName(departmentOptional.map(TDepartmentDO::getDepartmentName).orElse("默认部门名"));
        List<TDepartmentDO> list = this.list(new QueryWrapper<TDepartmentDO>().lambda().eq(TDepartmentDO::getParentId, 0).eq(TDepartmentDO::getIsDelete, 0));

        List<String> departmentList = list.stream()
                .map(departmentDO -> {
                    TDepartmentVO deptVO = new TDepartmentVO();
                    BeanUtil.copyProperties(departmentDO, deptVO);
                    return deptVO.getDepartmentName();
                })
                .collect(Collectors.toList());
        departmentStruVo.setList(departmentList);

        return departmentStruVo;

    }

    @Override
    public TDepartmentWorkerCountVO getDepartWorker() {

        return departmentDAO.getDepartWorker();
    }

    @Override
    public PropertyCountVO getPropertyCount() {
        return departmentDAO.getPropertyCount();
    }

    @Override
    public TDepartmentPositionVO getPosition() {
        return getDepartmentPositionIndex(1);
    }

    @Override
    public ZSalaryPayCountVO getPay(Integer year) {
        ZSalaryPayCountVO zSalaryPayCountVO = new ZSalaryPayCountVO();
        zSalaryPayCountVO.setYearWages(departmentDAO.getPay(year));

        zSalaryPayCountVO.setPositionList(departmentDAO.getMounthPay(year));

        return zSalaryPayCountVO;
    }

    @Override
    public List<ZDeputyActivityRecordDO> getList(Integer year) {

        return departmentDAO.getList(year);
    }


    public TDepartmentPositionVO getDepartmentPositionIndex(Integer i) {
        TDepartmentPositionVO positionVO = new TDepartmentPositionVO();
        TDepartmentDO tDepartmentDO = this.getById(i);
        if (tDepartmentDO == null) {
            tDepartmentDO = new TDepartmentDO();
        }

        positionVO.setDepartmentName(tDepartmentDO.getDepartmentName());
        positionVO.setDepartContent(tDepartmentDO.getDepartContent());
        positionVO.setDuty(tDepartmentDO.getDuty());

        // 获取所有子部门
        List<TDepartmentDO> departmentDOS = this.list(new QueryWrapper<TDepartmentDO>()
                .lambda()
                .eq(TDepartmentDO::getParentId, tDepartmentDO.getDepartmentId())
                .eq(TDepartmentDO::getIsDelete, 0));

        // 创建部门ID列表，包含当前部门的ID
        List<Integer> departmentIds = departmentDOS.stream()
                .map(TDepartmentDO::getDepartmentId)
                .collect(Collectors.toList());
        departmentIds.add(tDepartmentDO.getDepartmentId());

        // 获取属于这些部门的所有工作人员
        List<TDepartmentWorkerDO> tDepartmentWorkerDOS = workerDAO.selectList(new QueryWrapper<TDepartmentWorkerDO>()
                .lambda()
                .eq(TDepartmentWorkerDO::getIsDelete, 0)
                .in(TDepartmentWorkerDO::getDepartmentId, departmentIds));
        positionVO.setStaffNum(tDepartmentWorkerDOS.size());


            if (i == 0) {
                // 查询所有满足条件的工作人员数据
                long uniqueNameCount = workerDAO.selectList(new QueryWrapper<TDepartmentWorkerDO>()
                        .lambda()
                        .eq(TDepartmentWorkerDO::getIsDelete, 0)
                )
                        .stream()
                        .map(TDepartmentWorkerDO::getWorkerName)
                        .filter(Objects::nonNull)
                        .distinct()
                        .count();
                positionVO.setStaffNum((int) uniqueNameCount);
                positionVO.setDeptNum(this.list(new LambdaQueryWrapper<TDepartmentDO>()
                        .eq(TDepartmentDO::getParentId, 0))
                        .size());
            }


        // 确保 positionList 已经初始化
        positionVO.setPositionList(new ArrayList<>());

        // 递归处理子部门
        departmentDOS.forEach(departmentDO -> {
            positionVO.getPositionList().add(getDepartmentPositionIndex(departmentDO.getDepartmentId()));  // 递归调用
        });

        return positionVO;

    }

}




